package com.aduerr.v1shouyin.mvp.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
import com.aduerr.v1shouyin.App;
import com.aduerr.v1shouyin.mvp.activity.HomePageActivity;
import com.aduerr.v1shouyin.util.MessageEvent;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

/**
 * Created by fyf on 2018/6/4 0004.
 */

public class MyBroadcastReceiver extends BroadcastReceiver {


    private static final String TAG = "MyBroadcastReceiver****";
    private NotificationManager nm;
    private int badgeCount = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction());

        if(JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
//            Log.d(TAG, "JPush用户注册成功");

        } else if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {

            String jsonData = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if(null != jsonData && jsonData.length() > 2) {
                MessageEvent messageEvent = new MessageEvent("auto_print");
                messageEvent.setContent(jsonData);
                EventBus.getDefault().post(messageEvent);
            }

            Log.d(TAG, "接受到推送下来的通知" + bundle.getString(JPushInterface.EXTRA_ALERT));
//            String DaoMoney = bundle.getString(JPushInterface.EXTRA_ALERT);
//            if (DaoMoney != null && SharedPreferencesUtile.getYUYINData(App.getApp(),
//            "ischeck", false)) {
//                WordstoVoice voice = new WordstoVoice();
//                voice.toVoice(App.getApp(), DaoMoney);
//            }
            //讯飞语音
//            WordstoVoice voice = new WordstoVoice();
//            voice.toVoice(App.getApp(),bundle.getString(JPushInterface.EXTRA_ALERT));
//            receivingNotification(context,bundle);
//            badgeCount += 1;
//            ShortcutBadger.applyCount(App.getApp(), badgeCount);

            String DaoMoney = bundle.getString(JPushInterface.EXTRA_ALERT);
            if(DaoMoney != null && SharedPreferencesUtils.getYUYINData(App.getApp(),
                    "ischeck", true)) {
                WordstoVoice voice = new WordstoVoice();
                //                LogUtils.e(TAG, "22222222222222" + DaoMoney);
                voice.toVoice(App.getApp(), bundle.getString(JPushInterface.EXTRA_ALERT));
//                    Log.e(TAG, "onReceive: " + bundle.getString(JPushInterface
//                    .EXTRA_ALERT));
//                LogUtils.e(TAG, "1111111111111111111" + JPushInterface.EXTRA_ALERT);
                receivingNotification(context, bundle);
            }

        } else if(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
//            Log.d(TAG, "接受到推送下来的自定义消EXTRA_MESSAGE" + bundle.getString(JPushInterface
//            .EXTRA_MESSAGE));
//            //讯飞语音
//            String DaoMoney = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//            if (DaoMoney != null&& SharedPreferencesUtils.getYUYINData(App.getApp(),
//            "ischeck", true)) {
//                WordstoVoice voice = new WordstoVoice();
//                voice.toVoice(App.getApp(), bundle.getString(JPushInterface
//                .EXTRA_MESSAGE));
//            }

        } else if(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了�?�知");

            Intent i = new Intent(context, HomePageActivity.class);
            i.putExtras(bundle);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);

//            openNotification(context,bundle);

        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

    private void receivingNotification(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        Log.d(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        Log.d(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Log.d(TAG, "extras : " + extras);

    }

    private void openNotification(Context context, Bundle bundle) {
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String myValue = "";
        try {
            //当前的url的�?�是由，极光推�?�的服务传�?�过来的，http://www.saiermeng.com
//          {
//              url:"http://www.saiermeng.com";
//          }
            JSONObject extrasJson = new JSONObject(extras);
            myValue = extrasJson.optString("url");

        } catch(Exception e) {
            Log.w(TAG, "Unexpected: extras is not a valid json", e);
            return;
        }
       /* Intent mIntent = new Intent(context, MainActivity.class);
        mIntent.putExtra("url", myValue);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);*/
    }
}
